/*    
    OpenMheg - A free MHEG-5 library
    Copyright (C) 2008 Dharmesh Malam

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

using System;
using System.Collections.Generic;
using System.Text;
using Core.Model;
using System.Xml;

namespace Core.Parser.XMLParser
{
    public class TokenGroupBuilder
    {
        public static TokenGroup Build ( XmlElement node )
        {
            TokenGroup tokenGroup = new TokenGroup();
            IngredientBuilder.Build( node, tokenGroup );

            #region  Add our attributes

            XmlElement e;

            if ( ( e = node ["movement-table"] ) != null )
                BuildMovementTable( e, tokenGroup );
            
            if ( ( e = node ["token-group-items"] ) != null )
                BuildTokenGroupItems( e, tokenGroup );
            else 
                throw new Exception( "no token group items" );

            if ( ( e = node ["no-token-action-slots"] ) != null )
                BuildNoTokenGroupActionsSlots( e, tokenGroup );

            #endregion

            return tokenGroup;
        }

        private static void BuildNoTokenGroupActionsSlots ( XmlElement e, TokenGroup tokenGroup )
        {
            foreach ( XmlElement actionSlot in e )
            {
                if ( actionSlot.Name == "action-slot" )
                    tokenGroup.NoTokenActionSlots.Add( ActionBuilder.Build( actionSlot ) );
                else if ( actionSlot.Name == "null" )
                    tokenGroup.NoTokenActionSlots.Add( null );
                else
                    throw new Exception( "Unknown action slot" );
            }
        }

        private static void BuildMovementTable ( XmlElement node, TokenGroup tokenGroup )
        {

            List<List<int>> movementTable = new List<List<int>>();

            List<int> movementTableRow;
            foreach ( XmlElement row in node )
            {
                if ( row.Name == "movement-table-row" )
                {
                    movementTableRow = new List<int>();

                    foreach ( XmlElement col in row )
                        if ( col.Name == "movement" )
                            movementTableRow.Add( DecodeUtils.ParseInt( col.InnerText ) );
                        else
                            throw new Exception( "Unkown node, excepecting movement" );

                    movementTable.Add( movementTableRow );
                }
                else
                    throw new Exception( "Unkown node, excepecting movement-row" );

            }

            tokenGroup.MovementTable = movementTable;
        }

        private static void BuildTokenGroupItems ( XmlElement node, TokenGroup tokenGroup )
        {

            foreach ( XmlElement tgi in node )
            {
                TokenGroupItem tokenGroupItem = new TokenGroupItem();
                tokenGroupItem.ObjectRef = ObjectRefBuilder.Build( tgi ["a-visible"] );

                foreach ( XmlElement actionSlot in tgi ["action-slots"] )
                {
                    if ( actionSlot.Name == "action-slot" )
                        tokenGroupItem.ActionSlots.Add( ActionBuilder.Build( actionSlot ) );
                    else if ( actionSlot.Name == "null" )
                      tokenGroupItem.ActionSlots.Add( null ); 
                    else
                        throw new Exception( "Unknown action slot" );
                }

                tokenGroup.TokenGroupItems.Add( tokenGroupItem );
            }


        }
    }

}
